package com.mes.loco.aps.server.controller.aps;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.CoreService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSManuCapacityStep;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.CoreServiceImpl;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工序工时
 *
 * @author YouWang·Peng
 * 2021-7-15 17:04:45
 */
@RestController
@RequestMapping("/api/APSManuCapacityStep")
public class APSManuCapacityStepController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(APSManuCapacityStepController.class);

    final
    APSService wAPSService;

    final
    CoreService wCoreService;

    public APSManuCapacityStepController(APSService wAPSService, CoreService wCoreService) {
        this.wAPSService = wAPSService;
        this.wCoreService = wCoreService;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<APSManuCapacityStep> wServiceResult = wAPSService.APS_QueryManuCapacityStep(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));

            if (wLineID <= 0 || wPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "修程和工位必填!");
            }

            int wStatus = StringUtils.parseInt(request.getParameter("Status"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"));

            BMSEmployee wLoginUser = this.GetSession(request);

            ServiceResult<List<APSManuCapacityStep>> wServiceResult = wAPSService
                    .APS_QueryManuCapacityStepList(wLoginUser, wLineID, wPartID, wStatus, wActive);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            APSManuCapacityStep wCapacity = CloneTool.Clone(wParam.get("data"), APSManuCapacityStep.class);
            if (wCapacity == null)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            BMSEmployee wLoginUser = this.GetSession(request);

            if (wCapacity.ID <= 0) {
                wCapacity.CreatorID = wLoginUser.ID;
                wCapacity.CreateTime = Calendar.getInstance();
            } else {
                wCapacity.EditorID = wLoginUser.ID;
                wCapacity.EditTime = Calendar.getInstance();
            }

            // 权限控制
            if (Configuration.readConfigString("EnableAccessControl", "config/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            502804, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            ServiceResult<Integer> wServiceResult = wAPSService.APS_UpdateManuCapacityStep(wLoginUser, wCapacity);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量激活或禁用
     */
    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            if (!wParam.containsKey("Active"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            int wActive = StringUtils.parseInt(wParam.get("Active"));

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            List<APSManuCapacityStep> wList = CloneTool.CloneArray(wParam.get("data"), APSManuCapacityStep.class);
            if (wList == null || wList.size() <= 0)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            List<Integer> wIDList = wList.stream().map(APSManuCapacityStep::getID).collect(Collectors.toList());

            // 权限控制
            if (wActive == 1 && Configuration.readConfigString("EnableAccessControl", "config/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            502802, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            } else if ((wActive == 0 || wActive == 2) && Configuration.readConfigString("EnableAccessControl",
                    "config/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            502803, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            wAPSService.APS_ActiveManuCapacityStepList(wLoginUser, wIDList, wActive);

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, null);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
